﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

public partial class Product_UOM_Maintain : PageBase
{
    private int id=0;
    private int productID = 0;
    private const string REQUEST_PRODUCT_ID = "productID";
    private const string REQUST_PRODUCT_UOM_ID="id";
    private const string RESPONSE_RETURN_PAGE = "Product_detail.aspx?id=";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUEST_PRODUCT_ID] != null)
        {
            int.TryParse(Request.QueryString[REQUEST_PRODUCT_ID], out productID);
        }
        if (Request.QueryString[REQUST_PRODUCT_UOM_ID] != null)
        {
            int.TryParse(Request.QueryString[REQUST_PRODUCT_UOM_ID], out id);
        }
        if (!IsPostBack )
        {
            //GetUomsType();
            this.FillForm();
        }
    }

    //private void GetUomsType()
    //{
    //    ddlUoms.DataSource = UomsManager.GetUoms(MyStateManager.DomainID);
    //    ddlUoms.DataTextField = "UomsDescription";
    //    ddlUoms.DataValueField = "UomsCode";
    //    ddlUoms.DataBind();
    //    ddlUoms.Items.Insert(0, new ListItem("--请选择--", ""));
    //}

    private void FillForm()
    {
        if (id>0)
        {
            Product_UomsManager manager = new Product_UomsManager();
            PRODUCT_UOMS uom = manager.Load(id);
            if (uom != null)
            {
                ddlUoms.SelectedValue = uom.UomsCode;
                txtNum.Text = uom.DENOMINATOR.ToString();
                cbxIsValid.Checked = uom.ACTIVE;
                txtWeight.Text = uom.WEIGHT.ToString();
                txtVol.Text = uom.VOLUME.ToString();
                cbxSelllFlag.Checked = uom.IsSell;
            }
        }
    }

    private void SaveData()
    {
        Product_UomsManager manager = new Product_UomsManager();
        PRODUCT_UOMS uom = null;
        if (id > 0)
        {
            uom = manager.Load(id);
        }
        else
        {
            uom = new PRODUCT_UOMS();
            uom.PRODUCTID = this.productID;
        }
        uom.UomsCode = ddlUoms.SelectedValue;
        uom.DENOMINATOR = decimal.Parse(txtNum.Text);
        uom.ACTIVE = cbxIsValid.Checked;
        uom.WEIGHT = decimal.Parse(txtWeight.Text);
        uom.VOLUME = decimal.Parse(txtVol.Text);
        uom.SELLFLAG = cbxSelllFlag.Checked ? "1" : "0";
        uom.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));

        uom.LAST_UPDATER = MyStateManager.Name;
        uom.DOMAINID = MyStateManager.DomainID;

        manager.Save(uom);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SaveData();
        Response.Redirect(RESPONSE_RETURN_PAGE + productID.ToString());
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(RESPONSE_RETURN_PAGE + productID.ToString());
    }
}
